<!-- 人口统计学信息 生活习惯 LifestyleHabits -->
<template>
  <div class="page-center">
    <div class="medical-form">
      <el-form :model="formData" label-width="140px" class="info-form" label-position="left">
        <el-divider content-position="left">
          <h3>生活习惯</h3>
        </el-divider>

        <el-row :gutter="12">
          <!-- 吸烟史 -->
          <el-col :span="12">
            <el-card class="box-card" shadow="never">
              <div slot="header" class="clearfix">
                <span>吸烟史</span>
              </div>
              <el-form-item label="吸烟状况">
                <div class="display-value info-value">{{ display(formData.smokingHistory.status) }}</div>
              </el-form-item>
              <el-form-item label="吸烟年限/时长">
                <div class="display-value info-value">{{ display(formData.smokingHistory.duration) }}</div>
              </el-form-item>
              <el-form-item label="吸烟量">
                <div class="display-value info-value">{{ display(formData.smokingHistory.amount) }}</div>
              </el-form-item>
              <el-form-item label="戒烟年数">
                <div class="display-value info-value">{{ display(formData.smokingHistory.quitYears) }}</div>
              </el-form-item>
            </el-card>
          </el-col>

          <!-- 饮酒史 -->
          <el-col :span="12">
            <el-card class="box-card" shadow="never">
              <div slot="header" class="clearfix">
                <span>饮酒史</span>
              </div>
              <el-form-item label="饮酒状况">
                <div class="display-value info-value">{{ display(formData.alcoholHistory.status) }}</div>
              </el-form-item>
              <el-form-item label="频率">
                <div class="display-value info-value">{{ display(formData.alcoholHistory.frequency) }}</div>
              </el-form-item>
              <el-form-item label="饮酒种类">
                <div class="display-value info-value">{{ display(formData.alcoholHistory.type) }}</div>
              </el-form-item>
              <el-form-item label="饮酒量">
                <div class="display-value info-value">{{ display(formData.alcoholHistory.amount) }}</div>
              </el-form-item>
            </el-card>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
import { habits } from '@/api/dataEntry/demographicInformation'

export default {
  name: 'LifestyleHabits',
  data() {
    return {
      formData: {
        patientId: null,
        smokingHistory: {
          status: '',
          duration: '',
          amount: '',
          quitYears: ''
        },
        alcoholHistory: {
          status: '',
          frequency: '',
          type: '',
          amount: ''
        }
      },
      loading: false
    }
  },
  computed: {
    ...mapGetters('patient', ['robustPatientId', 'patientFallbackData'])
  },
  watch: {
    robustPatientId: {
      handler(newId) {
        if (newId) {
          this.formData.patientId = newId
          this.loadData(newId)
        } else {
          this.resetForm()
        }
      },
      immediate: true
    }
  },
  async mounted() {
    if (!this.robustPatientId) {
      const restored = await this.restorePatientFromStorage()
      if (restored) {
        this.formData.patientId = restored.id || restored.patientId
        this.loadData(this.formData.patientId)
      }
    }
  },
  methods: {
    ...mapActions('patient', ['restorePatientFromStorage']),

    async loadData(patientId) {
      if (this.loading) return

      this.loading = true
      try {
        const res = await habits({ recordId: patientId })
        this.handleResponseData(res)
      } catch (error) {
        console.error('Failed to load lifestyle habits:', error)
        this.applyFallbackData()
      } finally {
        this.loading = false
      }
    },

    handleResponseData(res) {
      const payload = res?.data?.data ?? res?.data ?? res ?? {}

      this.formData.patientId = payload.patientId ?? this.robustPatientId

      // 处理吸烟史数据
      if (payload.smokingHistory) {
        this.processSmokingHistory(payload.smokingHistory)
      }

      // 处理饮酒史数据
      if (payload.alcoholHistory) {
        this.processAlcoholHistory(payload.alcoholHistory)
      }
    },

    processSmokingHistory(smokingData) {
      if (typeof smokingData === 'object') {
        this.formData.smokingHistory = {
          status: smokingData.status ?? '',
          duration: smokingData.duration ?? '',
          amount: smokingData.amount ?? '',
          quitYears: smokingData.quitYears ?? ''
        }
      } else {
        this.formData.smokingHistory.status = String(smokingData)
      }
    },

    processAlcoholHistory(alcoholData) {
      if (typeof alcoholData === 'object') {
        this.formData.alcoholHistory = {
          status: alcoholData.status ?? '',
          frequency: alcoholData.frequency ?? '',
          type: alcoholData.type ?? '',
          amount: alcoholData.amount ?? ''
        }
      } else {
        this.formData.alcoholHistory.status = String(alcoholData)
      }
    },

    applyFallbackData() {
      const patient = this.patientFallbackData
      if (patient) {
        // 吸烟史
        const smoking = patient.smokingHistory ?? patient.smokeHistory ?? patient.smoking
        if (smoking) this.processSmokingHistory(smoking)

        // 饮酒史
        const alcohol = patient.alcoholHistory ?? patient.drinkingHistory ?? patient.alcohol
        if (alcohol) this.processAlcoholHistory(alcohol)
      }
    },

    resetForm() {
      this.formData = {
        patientId: null,
        smokingHistory: {
          status: '',
          duration: '',
          amount: '',
          quitYears: ''
        },
        alcoholHistory: {
          status: '',
          frequency: '',
          type: '',
          amount: ''
        }
      }
    },

    display(val) {
      if (val === null || val === undefined || String(val).trim() === '') return '-'
      return val
    }
  }
}
</script>

<!-- 引入共享样式 -->
<style src="@/styles/shared.css"></style>

<style scoped>
</style>
